package com.ruoyi.system.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.io.FileUtil;
import com.sini.com.spire.doc.Document;
import com.sini.com.spire.doc.FileFormat;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ConferencePapers;
import com.ruoyi.system.service.IConferencePapersService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;



import static cn.hutool.core.io.FileUtil.writeFromStream;


/**
 * 会议文件Controller
 * 
 * @author ruoyi
 * @date 2023-12-19
 */
@Controller
@RequestMapping("/system/papers")
public class ConferencePapersController extends BaseController
{
    private String prefix = "system/papers";
    @Value("${ruoyi.filePath}")
    private String sysFilePath;
    @Autowired
    private IConferencePapersService conferencePapersService;

    @RequiresPermissions("system:papers:view")
    @GetMapping()
    public String papers()
    {
        return prefix + "/papers";
    }

    /**
     * 查询会议文件列表
     */
    @RequiresPermissions("system:papers:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConferencePapers conferencePapers)
    {
        startPage();
        List<ConferencePapers> list = conferencePapersService.selectConferencePapersList(conferencePapers);
        return getDataTable(list);
    }

    /**
     * 导出会议文件列表
     */
    @RequiresPermissions("system:papers:export")
    @Log(title = "会议文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConferencePapers conferencePapers)
    {
        List<ConferencePapers> list = conferencePapersService.selectConferencePapersList(conferencePapers);
        ExcelUtil<ConferencePapers> util = new ExcelUtil<ConferencePapers>(ConferencePapers.class);
        return util.exportExcel(list, "会议文件数据");
    }

    /**
     * 新增会议文件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存会议文件
     */
    @RequiresPermissions("system:papers:add")
    @Log(title = "会议文件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(List<MultipartFile> file) throws IOException
    {
        //遍历文件夹
        file.forEach(item ->{
            //获取文件名称
            String fileNewUrl = "/"+item.getOriginalFilename();
            try {
                //复制文件
                writeFromStream(item.getInputStream(),sysFilePath+"/"+item.getOriginalFilename());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if(Objects.equals(FileUtil.extName(item.getOriginalFilename()),"doc")||Objects.equals(FileUtil.extName(item.getOriginalFilename()),"docx")){
               //新建文件实体类
                Document document = new Document();
                //加载文件
                document.loadFromFile(sysFilePath+"/"+item.getOriginalFilename());
                //生成pdfurl
                String pdfUrl = (sysFilePath+"/"+item.getOriginalFilename().replace(FileUtil.extName(item.getOriginalFilename()),"pdf"));
                //替换文件后缀
                fileNewUrl = (fileNewUrl.replace(FileUtil.extName(item.getOriginalFilename()),"pdf"));
                //转换word为pdf
                document.saveToFile(pdfUrl, FileFormat.PDF);
            }

            ConferencePapers conferencePapers = new ConferencePapers();
            conferencePapers.setPapersName(FileUtil.mainName(item.getOriginalFilename()));
            conferencePapers.setPapersOldType(FileUtil.extName(item.getOriginalFilename()));
            conferencePapers.setPapersNewType(FileUtil.extName(fileNewUrl));
            conferencePapers.setPapersOldUrl("/"+item.getOriginalFilename());
            conferencePapers.setPapersNewUrl(fileNewUrl);
            conferencePapersService.insertConferencePapers(conferencePapers);
        });

        return toAjax(1);
    }

    /**
     * 修改会议文件
     */
    @RequiresPermissions("system:papers:edit")
    @GetMapping("/edit/{papersId}")
    public String edit(@PathVariable("papersId") Long papersId, ModelMap mmap)
    {
        ConferencePapers conferencePapers = conferencePapersService.selectConferencePapersByPapersId(papersId);
        mmap.put("conferencePapers", conferencePapers);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议文件
     */
    @RequiresPermissions("system:papers:edit")
    @Log(title = "会议文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConferencePapers conferencePapers)
    {
        return toAjax(conferencePapersService.updateConferencePapers(conferencePapers));
    }

    /**
     * 删除会议文件
     */
    @RequiresPermissions("system:papers:remove")
    @Log(title = "会议文件", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        System.out.println(ids);
        return toAjax(conferencePapersService.deleteConferencePapersByPapersIds(ids));
    }
}
